package cn.ywang.client;

import cn.ywang.model.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Auther: BeatificWang
 * @Date: 2018/11/25 23:29
 */
public class RemoteInvocationHandler implements InvocationHandler {
	private String host;
	private int port;

	public RemoteInvocationHandler(String host, int port) {
		this.host = host;
		this.port = port;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// 组装请求
		RpcRequest request = new RpcRequest();
		request.setClassName(method.getDeclaringClass().getName());
		request.setMethodName(method.getName());
		request.setArgs(args);
		// 通过TCP协议传输
		TCPTransport tcpTransport = new TCPTransport(this.host, this.port);
		// 发送请求
		return tcpTransport.send(request);
	}
}
